import 'package:flutter_admin/pages/common/cry/model/page_model.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/utils/kwargs.dart';
import 'package:flutter_admin/utils/odoo_http_util.dart';

class MrpProductionOdooApi {
  static Future<ResponseBodyApi> page({data}) async {
    ResponseBodyApi responseBodyApi;
    try {
      PageModel pageModel = data['page'];
      List domainList = data['domainList'];
      Kwargs recordsKwargs = Kwargs(
          domain: domainList,
          limit: pageModel.size,
          offset: (pageModel.current - 1) * pageModel.size,
          order: pageModel.orders[0].column! +
              " " +
              (pageModel.orders[0].asc! ? "asc" : "desc"));
      var records = await OdooHttpUtil.callKw(
          model: 'mrp.production',
          method: 'mark_retrospect',
          kwargs: recordsKwargs.toMap());
      var total = await OdooHttpUtil.callKw(
          model: 'mrp.production',
          method: 'mark_retrospect_count',
          kwargs: {"domain": domainList});
      List<Map<String, dynamic>> list = [];
      records.forEach((element) {
        Map<String, dynamic> map = element;
        list.add(map);
      });
      pageModel.records = list;
      pageModel.total = total;
      pageModel.pages = total ~/ pageModel.size + 1;
      responseBodyApi = ResponseBodyApi(success: true, data: pageModel);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> productionIssueBoard() async {
    ResponseBodyApi responseBodyApi;
    try {
      var records = await OdooHttpUtil.callKw(
          model: 'mrp.production',
          method: 'production_issue_board',
          kwargs: {"domain": {}});
      responseBodyApi = ResponseBodyApi(success: true, data: records);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> pricepage({data}) async {
    ResponseBodyApi responseBodyApi;
    try {
      PageModel pageModel = data['page'];
      List code = [
        ['product_id.default_code', '=', data['code']]
      ];
      Kwargs recordsKwargs = Kwargs(
        domain: code,
        limit: pageModel.size,
        offset: (pageModel.current - 1) * pageModel.size,
        // order: pageModel.orders[0].column! +
        //     " " +
        //     (pageModel.orders[0].asc! ? "asc" : "desc"),
      );
      var records = await OdooHttpUtil.callKw(
          model: 'price.trend_origin',
          method: 'search_product',
          kwargs: recordsKwargs.toMap());
      var total = await OdooHttpUtil.callKw(
          model: 'price.trend_origin',
          method: 'search_product_count',
          kwargs: {"domain": code});
      List<Map<String, dynamic>> list = [];
      records.forEach((element) {
        Map<String, dynamic> map = element;
        list.add(map);
      });
      pageModel.records = list;
      pageModel.total = total;
      pageModel.pages = total ~/ pageModel.size + 1;
      responseBodyApi = ResponseBodyApi(success: true, data: pageModel);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> toCreate(data) async {
    ResponseBodyApi responseBodyApi;
    try {
      await OdooHttpUtil.callKw(
          model: 'price.trend_origin',
          method: 'to_create',
          kwargs: {"domain": data});
      responseBodyApi = ResponseBodyApi(success: true);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> addProduct(data) async {
    ResponseBodyApi responseBodyApi;
    try {
      await OdooHttpUtil.callKw(
          model: 'price.trend_origin',
          method: 'add_product',
          kwargs: {'domain': data});
      responseBodyApi = ResponseBodyApi(success: true);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> pageDetail({data}) async {
    ResponseBodyApi responseBodyApi;
    try {
      PageModel pageModel = data['page'];
      List domainList = data['domainList'];
      Kwargs recordsKwargs = Kwargs(
          domain: domainList,
          limit: pageModel.size,
          offset: (pageModel.current - 1) * pageModel.size,
          order: pageModel.orders[0].column! +
              " " +
              (pageModel.orders[0].asc! ? "asc" : "desc"));
      var records = await OdooHttpUtil.callKw(
          model: 'price.trend',
          method: 'to_search',
          kwargs: recordsKwargs.toMap());
      var total = await OdooHttpUtil.callKw(
          model: 'price.trend',
          method: 'to_search_count',
          kwargs: {"domain": domainList});
      List<Map<String, dynamic>> list = [];
      records.forEach((element) {
        Map<String, dynamic> map = element;
        list.add(map);
      });
      pageModel.records = list;
      pageModel.total = total;
      pageModel.pages = total ~/ pageModel.size + 1;
      responseBodyApi = ResponseBodyApi(success: true, data: pageModel);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> getERP(data) async {
    ResponseBodyApi responseBodyApi;
    try {
      await OdooHttpUtil.callKw(
          model: 'price.trend',
          method: 'search_product',
          kwargs: {'domain': data});
      responseBodyApi = ResponseBodyApi(success: true);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> saveOrUpdate(data) async {
    ResponseBodyApi responseBodyApi;
    try {
      await OdooHttpUtil.callKw(
          model: 'price.trend', method: 'price_add', kwargs: {'domain': data});
      responseBodyApi = ResponseBodyApi(success: true);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }
  static Future<ResponseBodyApi> removeByIds(data) async {
    ResponseBodyApi responseBodyApi;
    try {
      await OdooHttpUtil.callKw(
          model: 'price.trend',
          method: "unlink",
          args: [data]);
      responseBodyApi = ResponseBodyApi(success: true);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }
}
